package classes.managers {
	
	import adobe.utils.CustomActions;
	import classes.controlers.Coin_Controler;
	import classes.controlers.Disrupt_Controler;
	import classes.controlers.Pointer_Controler;
	import classes.controlers.Rabbit_Controler;
	import classes.controlers.Timer_Controler;
	import classes.global.GlobalProperties;
	import classes.scenes.A_Scene;
	import classes.sounds.SoundCollection;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Game_Manager extends A_Scene {
		public var obtainCoin:int = 0;
		
		private var timer:Timer_Controler;
		private var disrupt:Disrupt_Controler;
		private var coins:Coin_Controler;
		private var pointer:Pointer_Controler;
		private var rabbit:Rabbit_Controler;
		
		private var isRollOver:Boolean = false;
		
		private var level:int = 1;
		
		public function Game_Manager( _main:Main, _mc:MovieClip ) {
			super( _main, _mc );
			
			timer = new Timer_Controler( this, mc.timer_mc );
			disrupt = new Disrupt_Controler( this, mc.disrupt_mc );
			coins = new Coin_Controler( this, mc.coins_mc );
			pointer = new Pointer_Controler( this, mc.pointer_mc );
			rabbit = new Rabbit_Controler( this, mc.rabbit_mc );
			
			addEvent();
		}
		
		public function init():void {
			mc.ssdam_mc.visible = true;
			mc.ssdam_mc.mouseEnabled = false;
			mc.disrupt_mc.mouseEnabled = false;
			
			obtainCoin = 0;
			level = 1;
			
			addEvent();
			
			coinInit( mc.coinNum_mc );
			
			timer.init();
			disrupt.init();
			coins.init();
			pointer.init( mc.coins_mc );
			rabbit.init();
		}
		
		private function ROLL_OVER(e:MouseEvent):void { 
			isRollOver = true; 
			if ( pointer.isDown ) {
				mc.ssdam_mc.visible = false;
				rabbit.sleep();
			}
			pointer.play();
		} 
		
		private function ROLL_OUT(e:MouseEvent):void { 
			if ( isRollOver ) {
				if ( pointer.isDown ) {
					rabbitWakeUp();
				}
				pointer.stop();
				isRollOver = false;
				mc.ssdam_mc.visible = true;
			}
		} 
		
		private function MOUSE_DOWN(e:MouseEvent):void { 
			main.stage.addEventListener(MouseEvent.MOUSE_UP, MOUSE_UP);
			
			pointer.isDown = true;
			mc.ssdam_mc.visible = false;
			rabbit.sleep();
			pointer.play(); 
			timer.start(); 
		} 
		
		private function MOUSE_UP(e:MouseEvent):void {
			main.stage.removeEventListener(MouseEvent.MOUSE_UP, MOUSE_UP );
			pointer.isDown = false;
			pointer.stop(); 
			//rabbitWakeUp();
			gameOver();
			mc.ssdam_mc.visible = true;
		}
		
		public function disruptStart( _num:int ):void {
			//disrupt.start( _num );
		}
		
		//10초카운트
		public function countUpdate( _num:int ):void {
			mc.count_txt.text = String( _num );
		}
		
		//방구끼기
		public function bang( _level:int ):void {
			level = _level;
			var random:int = Math.random() * 1000;
			
			if ( level < 7 ) {
				if ( random >= 0 && random < 2 ) { // 0.002%
					SoundCollection.bigFartSound();
					trace("bigBang")
					rabbit.bigBang();
				}else if ( random >= 10 && random < 20 ) { // 0.1%
					SoundCollection.bigFartSound();
					trace("middleBang")
					rabbit.middleBang();
				}else {
					SoundCollection.fartSound();
					trace("bang")
					rabbit.bang();
				}
			}else if( level >= 7 && level < 80 ) {
				if ( random >= 0 && random < 5 ) { // 0.005%
					SoundCollection.bigFartSound();
					trace("bigBang2")
					rabbit.bigBang();
				}else if ( random >= 10 && random < 20 ) { // 0.1%
					SoundCollection.bigFartSound();
					trace("middleBang2")
					rabbit.middleBang();
				}else {
					SoundCollection.fartSound();
					trace("bang2")
					rabbit.bang();
				}
			}
			
			pointer.coinTouch1 = false;
			pointer.coinTouch2 = false;
			pointer.coinTouch3 = false;
		}
		
		//방구 잭팟 끝.
		public function bigBang_END():void {
			coins.spawnCoin(10000);
		}
		
		//방구 준 잭팟 끝.
		public function middleBang_END():void {
			coins.spawnCoin(1000);
		}
		
		//방구끝 랜덤 코인.
		public function bang_END():void {
			var percent:int;
			var random:int;
			
			trace("bang_END level: ", level)
			
			switch (level) {
				case 1: 
					coins.spawnCoin(10);
				break;
				case 2:	 
					random = Math.random() * 2;
					if( random === 0 ) coins.spawnCoin(10);
					else coins.spawnCoin(20);
				break;
				case 3:	 
					percent = Math.random() * 10;
					if ( percent >= 0 && percent < 8 ) {
						random = Math.random() * 10;
						if ( random === 0 || random === 1 ) {
							coins.spawnCoin(10);
						}else if ( random === 2 || random === 3 || random === 4 ) {
							coins.spawnCoin(20);
						}else if ( random === 5 || random === 6 || random === 7 || random === 8 || random === 9 ) {
							coins.spawnCoin(30);
						}
					}
				break;
				case 4:	 
					percent = Math.random() * 10;
					if ( percent >= 0 && percent < 7 ) {
						random = Math.random() * 10;
						if ( random === 0 || random === 1 ) {
							coins.spawnCoin(10);
						}else if ( random === 2 || random === 3 || random === 4 ) {
							coins.spawnCoin(20);
						}else if ( random === 5 || random === 6 ) {
							coins.spawnCoin(30);
						}else if ( random === 7 || random === 8 || random === 9 ) {
							coins.spawnCoin(40);
						}
					}
					
				break;
				case 5:	 
						random = Math.random() * 2;
						if( random === 0 ) coins.spawnCoin(50);
						else coins.spawnCoin(60);
				break;
				case 6:	 
					percent = Math.random() * 10;
					if ( percent >= 0 && percent < 6 ) {
						random = Math.random() * 10;
						if ( random === 0 || random === 1 ) {
							coins.spawnCoin(10);
						}else if ( random === 2 ) {
							coins.spawnCoin(20);
						}else if ( random === 3 ) {
							coins.spawnCoin(30);
						}else if ( random === 4 ) {
							coins.spawnCoin(40);
						}else if ( random === 5 || random === 6 || random === 7 ) {
							coins.spawnCoin(50);
						}else if ( random === 8 || random === 9 ) {
							coins.spawnCoin(60);
						}
					}
				break;
				case 7:	 
					random = Math.random() * 5;
					if( random === 0 || random === 1 || random === 2 || random === 3 ) coins.spawnCoin(100);
					else coins.spawnCoin(60);
				break;
				case 8:	 
					trace("level 8 이상부터 : ", level)
					percent = Math.random() * 20;
					trace("확률 0-19중 1일때: ", percent)
					if ( percent == 1 ) {
						random = Math.random() * 20;
						if ( random === 0 || random === 1 || random === 3 || random === 4 ) {
							coins.spawnCoin(10);
						}else if ( random === 5 || random === 6 ) {
							coins.spawnCoin(20);
						}else if ( random === 7 || random === 8 ) {
							coins.spawnCoin(30);
						}else if ( random === 9 ) {
							coins.spawnCoin(40);
						}else if ( random === 10 ) {
							coins.spawnCoin(50);
						}else if ( random === 11 || random === 12 || random === 13 || random === 14 ) {
							coins.spawnCoin(60);
						}else if ( random === 15 || random === 16 || random === 17 || random === 18 || random === 19 || random === 20 ) {
							coins.spawnCoin(100);
						}
					}
				break;
				default:
			}
		}
		
		
		//코인 얻기
		public function getCoin( _str:String ):void {
			if ( _str == "coin1" ) {
				coins.coin1 = true;
			}else if ( _str == "coin2" ) {
				coins.coin2 = true;
			}else if ( _str == "coin3" ) {
				coins.coin3 = true;
			}
			
			coins.obtainCoin();
			coinCount( obtainCoin, mc.coinNum_mc );
		}
		
		private function coinCount( _cnt:int, _mc:MovieClip ):void {
			var str:String;
			var num10000:String;
			var num1000:String;
			var num100:String;
			var num10:String;
			var num1:String;
			
			if ( _cnt < 10 ) {
				_mc.num1_mc.gotoAndStop( _cnt + 1 );
			}else if ( _cnt >= 10 && _cnt < 100 ) {
				str = String( _cnt );
				num10 = str.substr(0, 1);
				num1 = str.substr(1, 1);
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 100 && _cnt < 1000 ) {
				str = String( _cnt );
				num100 = str.substr(0, 1);
				num10 = str.substr(1, 1);
				num1 = str.substr(2, 1);
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 1000 && _cnt < 10000 ) {
				str = String( _cnt );
				num1000 = str.substr(0, 1);
				num100 = str.substr(1, 1);
				num10 = str.substr(2, 1);
				num1 = str.substr(3, 1);
				
				if( Number(num1000) === 0 ) _mc.num1000_mc.gotoAndStop( 1 );
				else _mc.num1000_mc.gotoAndStop( Number(num1000) + 1 );
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 10000 && _cnt < 100000 ) {
				str = String( _cnt );
				num10000 = str.substr(0, 1);
				num1000 = str.substr(1, 1);
				num100 = str.substr(2, 1);
				num10 = str.substr(3, 1);
				num1 = str.substr(4, 1);
				
				if( Number(num10000) === 0 ) _mc.num10000_mc.gotoAndStop( 1 );
				else _mc.num10000_mc.gotoAndStop( Number(num10000) + 1 );
				
				if( Number(num1000) === 0 ) _mc.num1000_mc.gotoAndStop( 1 );
				else _mc.num1000_mc.gotoAndStop( Number(num1000) + 1 );
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}
		}
		
		private function coinInit( _mc:MovieClip ):void {
			_mc.num10000_mc.gotoAndStop( 1 );
			_mc.num1000_mc.gotoAndStop( 1 );
			_mc.num100_mc.gotoAndStop( 1 );
			_mc.num10_mc.gotoAndStop( 1 );
			_mc.num1_mc.gotoAndStop( 1 );
		}
		
		//토끼 일어나기
		public function rabbitWakeUp():void {
			rabbit.wakeUp();
		}
		//게임오버
		public function gameOver():void {
			removeEvent();
			timer.stop();
			pointer.gameOver();
			rabbit.gameOver();
			
			SoundCollection.gameOverSound();
			GlobalProperties.score = timer.day + timer.hour + timer.min + timer.sec + timer.milSecond;
			
			trace("score : ", GlobalProperties.score)
			
			main.viewChange("gameOver");
			main.gameOverInit();
		}
		
		private function removeEvent():void {
			mc.rabbit_mc.removeEventListener(MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
			mc.rabbit_mc.cover_mc.removeEventListener(MouseEvent.ROLL_OVER, ROLL_OVER  );
			mc.rabbit_mc.cover_mc.removeEventListener(MouseEvent.ROLL_OUT, ROLL_OUT);
			
			if ( main.stage.hasEventListener(MouseEvent.MOUSE_UP )) {
				main.stage.removeEventListener(MouseEvent.MOUSE_UP, MOUSE_UP );
			}
		}
		
		private function addEvent():void {
			mc.rabbit_mc.addEventListener(MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
			mc.rabbit_mc.cover_mc.addEventListener(MouseEvent.ROLL_OVER, ROLL_OVER  );
			mc.rabbit_mc.cover_mc.addEventListener(MouseEvent.ROLL_OUT, ROLL_OUT);
		}
		
	}
}